﻿import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + "\ConfigurationManager.psd1")

. "$PSScriptRoot\MaintenanceWindows.ps1"

Function Create-CMDeviceCollectionQuery
{
param
(
    [Parameter(Mandatory=$true)]$DeviceCollectionName,
    [Parameter(Mandatory=$true)]$RuleName,
    [Parameter(Mandatory=$true)]$QueryExpression,
    [Parameter(Mandatory=$true)]$PSDrive
)
    cd ($PSDrive+":")
    if((Find-CMDeviceCollectionQuery -DeviceCollectionName $DeviceCollectionName -RuleName $RuleName -PSDrive $PSDrive ) -eq $false)
    {
        Add-CMDeviceCollectionQueryMembershipRule -CollectionName $DeviceCollectionName -QueryExpression $QueryExpression -RuleName $RuleName
    }
}

Function Find-CMDeviceCollectionQuery
{
param
(
    [Parameter(Mandatory=$true)]$DeviceCollectionName,
    [Parameter(Mandatory=$true)]$RuleName,
    [Parameter(Mandatory=$true)]$PSDrive
)
    cd ($PSDrive+":")
    
    $DeviceCollectionQueryMembershipRule = Get-CMDeviceCollectionQueryMembershipRule -CollectionName $DeviceCollectionName -RuleName $RuleName
    if($DeviceCollectionQueryMembershipRule -eq $null -or $DeviceCollectionQueryMembershipRule -eq "")
    {
        #Does not exist, can be created
        $false
    }
    else
    {
        $true
    }
}


Function Create-CMDeviceCollection
{
param
(
    [Parameter(Mandatory=$true)]$DeviceCollectionName,
    [Parameter(Mandatory=$true)]$LimitingCollectionName,
    [Parameter(Mandatory=$true)]$PSDrive,
    [Parameter(Mandatory=$true)]$CollectionRefreshSchedule,
    [Parameter(Mandatory=$true)]$FolderPathForCollections
)
    cd ($PSDrive+":")

    if ((Find-CMDeviceCollection -DeviceCollectionName $DeviceCollectionName -PSDrive $PSDrive) -eq $false )
    {#collection does not exist
        
        #Create needed CMSchedule Object from $CollectionRefreshSchedule
        $CMSchedule = Create-CMScheduleParameters -CMScheduleParameters $CollectionRefreshSchedule

        #Create Collection create it
        $CMDeviceCollection = New-CMDeviceCollection -Name $DeviceCollectionName -LimitingCollectionName $LimitingCollectionName -RefreshSchedule $CMSchedule
        
        
        if($FolderPathForCollections -ne $null)
        {#Move the new collection to it's designated Folder
            Move-CMObject -FolderPath ($PSDrive+":"+$FolderPathForCollections) -InputObject $CMDeviceCollection
        }

        $false
    }
    else
    {
        $true
    }
}

Function Find-CMDeviceCollection
{
param
(
    [Parameter(Mandatory=$true)]$DeviceCollectionName,
    [Parameter(Mandatory=$true)]$PSDrive
)
    cd ($PSDrive+":")

    $DeviceCollection = Get-CMDeviceCollection -Name $DeviceCollectionName
    if($DeviceCollection -eq $null -or $DeviceCollection -eq "")
    {
        #Does not exist, can be created
        $false
    }
    else
    {
        $true
    }
}


Function Create-CMDeviceCollections
{
param
(
    [Parameter(Mandatory=$true)][xml]$xmlDocumentWithConfiguration #= $xmlConfiguration
)

    $SoftwareUpdateDeployments = $xmlDocumentWithConfiguration.ADRs.SoftwareUpdateDeployments.SoftwareUpdateDeployment
    foreach($SoftwareUpdateDeployment in $SoftwareUpdateDeployments)
    {

        #create Collection if missing
        Create-CMDeviceCollection -DeviceCollectionName $SoftwareUpdateDeployment.CollectionName -LimitingCollectionName $SoftwareUpdateDeployment.LimitingCollectionName -PSDrive $xmlDocumentWithConfiguration.ADRs.Common.PSDrive -CollectionRefreshSchedule $SoftwareUpdateDeployment.CollectionRefreshSchedule -FolderPathForCollections $SoftwareUpdateDeployment.FolderPath
        
        $DeviceCollectionQueries = $SoftwareUpdateDeployment.DeviceCollectionQueries.DeviceCollectionQuery
        foreach($DeviceCollectionQuery in $DeviceCollectionQueries)
        {
            Create-CMDeviceCollectionQuery -DeviceCollectionName $SoftwareUpdateDeployment.CollectionName -RuleName (($DeviceCollectionQuery.RuleName).replace($SoftwareUpdateDeployment.DeviceCollectionQueryReplaceWhat, $SoftwareUpdateDeployment.CollectionName)) -QueryExpression (($DeviceCollectionQuery.QueryExpression).replace($SoftwareUpdateDeployment.DeviceCollectionQueryReplaceWhat,$SoftwareUpdateDeployment.CollectionName)) -PSDrive $xmlDocumentWithConfiguration.ADRs.Common.PSDrive
        }

        Create-MaintenanceWindows -xmlDocumentWithConfiguration $xmlDocumentWithConfiguration
    }
}